package cn.fanyamin.web.struts;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;

import cn.fanyamin.business.RoleService;
import cn.fanyamin.business.UserService;
import cn.fanyamin.domain.Role;
import cn.fanyamin.domain.User;

public class UserAction extends SecureBaseAction {
	private static Log logger = LogFactory.getLog(UserAction.class);

	public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
		UserService userService = getWebApp().getUserService();
	    List<User> userList = userService.list();
	    request.setAttribute("userList", userList);
		logger.info("userList: " + userList);
        return mapping.findForward("list");

	}
	public ActionForward create(ActionMapping mapping, ActionForm form,
	                            HttpServletRequest request, HttpServletResponse response) {
		UserService userService = getWebApp().getUserService();
		User user = getUserFromForm(form);
		if(user == null) {
		    return mapping.findForward("failure");
		}
		String pwd = DigestUtils.md5Hex(user.getPassword());
		user.setPassword(pwd);
		int userID = userService.create(user);
		user.setUserID(userID);
		logger.info("create user: " + user);
		return mapping.findForward("success");
	}
	
    private User getUserFromForm(ActionForm form) {
		User user = new User();
		LazyValidatorForm userForm = (LazyValidatorForm) form;

		user.setUserID(NumberUtils.toInt((String) userForm.get("userID")));
		// BeanUtils.copyProperties(user, form);
		user.setUserName((String) userForm.get("userName"));		
		user.setPassword((String) userForm.get("password"));
		user.setEmail((String) userForm.get("email"));
		user.setPhoneNumber((String) userForm.get("phoneNumber"));
		user.setRoleID(NumberUtils.toInt((String)userForm.get("roleID")));
		return user;
	}

    public ActionForward retrieve(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        UserService userService = getWebApp().getUserService();
        LazyValidatorForm userFrom = (LazyValidatorForm)form;
        int userID = NumberUtils.toInt((String)userFrom.get("userID"));
        User user = userService.retrieve(userID);
        request.setAttribute("user", user);
            
        return mapping.findForward("edit");
    }
    
    public ActionForward update(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        
        UserService userService = getWebApp().getUserService();
        User user = getUserFromForm(form);
        String pwd = DigestUtils.md5Hex(user.getPassword());
		user.setPassword(pwd);
        userService.update(user);
        logger.info("update user, user=" + user);
        return mapping.findForward("success");
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        UserService userService = getWebApp().getUserService();
        LazyValidatorForm userFrom = (LazyValidatorForm)form;
        int userID = NumberUtils.toInt((String)userFrom.get("userID"));
        userService.delete(userID);
        logger.info("delete user, userID=" + userID);
        return mapping.findForward("success");
    }
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        RoleService roleService = getWebApp().getRoleService();
	    List<Role> roleList = roleService.list();
	    request.setAttribute("roleList", roleList);
	    
    	return mapping.findForward("add");
    }
    
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        UserService userService = getWebApp().getUserService();
        LazyValidatorForm userFrom = (LazyValidatorForm)form;
        int userID = NumberUtils.toInt((String)userFrom.get("userID"));
        User user = userService.retrieve(userID);
        request.setAttribute("user", user);
        
        RoleService roleService = getWebApp().getRoleService();
	    List<Role> roleList = roleService.list();
	    request.setAttribute("roleList", roleList);
	    
        return mapping.findForward("edit");
    }
}
